package com.ruoyi.yp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.yp.domain.bo.BsYpBo;
import com.ruoyi.yp.domain.vo.BsYpVo;
import com.ruoyi.yp.domain.BsYp;
import com.ruoyi.yp.mapper.BsYpMapper;
import com.ruoyi.yp.service.IBsYpService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 样品Service业务层处理
 *
 * @author zyb
 * @date 2024-03-29
 */
@RequiredArgsConstructor
@Service
public class BsYpServiceImpl implements IBsYpService {

    private final BsYpMapper baseMapper;

    /**
     * 查询样品
     */
    @Override
    public BsYpVo queryById(Long ypId){
        return baseMapper.selectVoById(ypId);
    }

    /**
     * 查询样品列表
     */
    @Override
    public TableDataInfo<BsYpVo> queryPageList(BsYpBo bo, PageQuery pageQuery) {
        if (ObjectUtils.isNotEmpty(bo.getSyType()) && "0".equals(bo.getSyType())) {
            bo.setSyType(null);
        }
        Page<BsYpVo> result = baseMapper.page(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询样品列表
     */
    @Override
    public List<BsYpVo> queryList(BsYpBo bo) {
        if (ObjectUtils.isNotEmpty(bo.getSyType()) && "0".equals(bo.getSyType())) {
            bo.setSyType(null);
        }
        LambdaQueryWrapper<BsYp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsYp> buildQueryWrapper(BsYpBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsYp> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getYpNum()), BsYp::getYpNum, bo.getYpNum());
        lqw.eq(ObjUtil.isNotNull(bo.getDeptId()), BsYp::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getYpClass()), BsYp::getYpClass, bo.getYpClass());
        lqw.eq(bo.getTzId() != null, BsYp::getTzId, bo.getTzId());
        lqw.eq(StringUtils.isNotBlank(bo.getYpDescribe()), BsYp::getYpDescribe, bo.getYpDescribe());
        lqw.eq(StringUtils.isNotBlank(bo.getGcbw()), BsYp::getGcbw, bo.getGcbw());
        lqw.eq(StringUtils.isNotBlank(bo.getFbfxmc()), BsYp::getFbfxmc, bo.getFbfxmc());
        lqw.eq(bo.getYpDate() != null, BsYp::getYpDate, bo.getYpDate());
        lqw.eq(StringUtils.isNotBlank(bo.getYpPerson()), BsYp::getYpPerson, bo.getYpPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getYpStatus()), BsYp::getYpStatus, bo.getYpStatus());
        lqw.eq(ObjUtil.isNotNull(bo.getSyType()), BsYp::getSyType, bo.getSyType());
        lqw.orderByAsc(BsYp::getDeptId);
        lqw.orderByDesc(BsYp::getYpDate);
        return lqw;
    }

    /**
     * 新增样品
     */
    @Override
    public Boolean insertByBo(BsYpBo bo) {
        BsYp add = BeanUtil.toBean(bo, BsYp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setYpId(add.getYpId());
        }
        return flag;
    }

    /**
     * 修改样品
     */
    @Override
    public Boolean updateByBo(BsYpBo bo) {
        BsYp update = BeanUtil.toBean(bo, BsYp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsYp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除样品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
